package com.tc.dispatch.web.schedule.cost;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import com.tc.dispatch.csservice.ejb.CSServiceLocal;
import com.tc.dispatch.csservice.exception.CSException;
import com.tc.dispatch.gsservice.ejb.GSServiceLocal;
import com.tc.dispatch.gsservice.exception.GSException;
import com.tc.dispatch.gsservice.model.ExRate;
import com.tc.dispatch.web.csservice.csHelper;
import com.tc.dispatch.web.gsservice.gsHelper;
import com.tc.dispatch.web.util.DateJsonValueProcessor;
import com.tc.dispatch.web.util.FloatJsonValueProcessor;

public class MaoPiDanJia extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public MaoPiDanJia() {
		super();
	}
	
	public boolean removeRecord(int id)
	{
		CSServiceLocal css=csHelper.getCSServiceLocal();
		try{
		css.removeMaoPi(id);
		}
		catch(Exception e)
		{
			System.out.println("remove error:"+e.getMessage());
			return false;
		}
		return true;
	}
	public List getMaoPi(String ssign)
	{
		CSServiceLocal css=csHelper.getCSServiceLocal();
		try{
			int year1=Calendar.getInstance().getTime().getYear()+1900;
			int month1=Calendar.getInstance().getTime().getMonth()+1;
			String whereStr=" where ssign='"+ssign+"' and month1="+month1+" and year1="+year1;
			System.out.println("month:"+month1+"::year1:"+year1+"::whereStr:"+whereStr);
			
			com.tc.dispatch.csservice.model.MaoPiDanJia mp= css.findMaoPi(whereStr);
			List ret=new ArrayList();
			ret.add(mp.getFangkuai());
			ret.add(mp.getYz_1());
			ret.add(mp.getYz_2());
			ret.add(mp.getYz_3());
			ret.add(mp.getYz_4());
			ret.add(mp.getYz_5());
			ret.add(mp.getYz_6());
			ret.add(mp.getYz_7());
			return ret;
		}
		catch(CSException e)
		{
			System.out.println("CSException:"+e.getMessage());
			return null;
		}
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String action = request.getParameter("action");
		if (action == null)
			action = "";

		CSServiceLocal css = csHelper.getCSServiceLocal();
		HttpSession sess = request.getSession();
		if (action.equals("list")) {
			String page = request.getParameter("start");
			if (page == null)
				page = "0";
			String pagePerNum = request.getParameter("limit");
			if (pagePerNum == null)
				pagePerNum = "10";
			try {

				List ret = css.findMaoPiByPagesWhereStr(Integer.parseInt(page),
						Integer.parseInt(pagePerNum), "");

				JSONObject json = new JSONObject();
				JsonConfig cfg = new JsonConfig();
				cfg.registerJsonValueProcessor(java.util.Date.class,
						new DateJsonValueProcessor());
				cfg.registerJsonValueProcessor(java.sql.Date.class,
						new DateJsonValueProcessor());
				cfg.registerJsonValueProcessor(java.lang.Float.class, new FloatJsonValueProcessor());

				// BeanUtils.copyProperties();
				json.put("totalProperty", new Integer(css.getMPTotalNums("")));

				JSONArray arr = JSONArray.fromObject(ret.toArray(), cfg);
				json.put("root", arr);

				response.getWriter().print(json);
			} catch (CSException e) {

				System.out.println("list error:" + e.getMessage());
				response.getWriter().print("{success:false}");
			}

		} else if (action.equals("add")) {
			String ssign = request.getParameter("ssign" );
			String fangkuai = request.getParameter("fangkuai" );
			String yz_1 = request.getParameter("yz_1" );
			String yz_2 = request.getParameter("yz_2" );
			String yz_3 = request.getParameter("yz_3" );
			String yz_4 = request.getParameter("yz_4" );
			String yz_5 = request.getParameter("yz_5" );
			String yz_6 = request.getParameter("yz_6" );
			String yz_7 = request.getParameter("yz_7" );
			String month1=request.getParameter("month1");
			String year1=request.getParameter("year1");
			try {
				com.tc.dispatch.csservice.model.MaoPiDanJia mp=new com.tc.dispatch.csservice.model.MaoPiDanJia (0,ssign, Float.valueOf(fangkuai),
                        Float.valueOf(yz_1),
                        Float.valueOf(yz_2),
                        Float.valueOf(yz_3),
                        Float.valueOf(yz_4),
                        Float.valueOf(yz_5),
                        Float.valueOf(yz_6),
                        Float.valueOf(yz_7), Integer.parseInt(month1),
                        Integer.parseInt(year1));
				css.createMaoPi(mp);
			} catch (CSException e) {
				System.out.println("create error:" + e.getMessage());
			} catch (Exception e) {
				System.out.println("exception:" + e.getMessage());
			}
			out.print("{success:true, data:'sucess'}");
		} else if (action.equals("update")) {
			String id=request.getParameter("id");
			String ssign = request.getParameter("ssign" );
			String fangkuai = request.getParameter("fangkuai" );
			String yz_1 = request.getParameter("yz_1" );
			String yz_2 = request.getParameter("yz_2" );
			String yz_3 = request.getParameter("yz_3" );
			String yz_4 = request.getParameter("yz_4" );
			String yz_5 = request.getParameter("yz_5" );
			String yz_6 = request.getParameter("yz_6" );
			String yz_7 = request.getParameter("yz_7" );
			String month1=request.getParameter("month1");
			String year1=request.getParameter("year1");
			try {

				com.tc.dispatch.csservice.model.MaoPiDanJia mp=new com.tc.dispatch.csservice.model.MaoPiDanJia (0,ssign, Float.valueOf(fangkuai),
                        Float.valueOf(yz_1),
                        Float.valueOf(yz_2),
                        Float.valueOf(yz_3),
                        Float.valueOf(yz_4),
                        Float.valueOf(yz_5),
                        Float.valueOf(yz_6),
                        Float.valueOf(yz_7), Integer.parseInt(month1),
                        Integer.parseInt(year1));
				css.updateMaoPi(mp);
			} catch (CSException e) {
				System.out.println("update error:" + e.getMessage());
			} catch (Exception e) {
				System.out.println("exception:" + e.getMessage());
			}
			out.print("{success:true, data:'sucess'}");
		}
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
